package com.gouwudai.gobbs.html;

import java.io.IOException;
import java.util.Date;

import javax.servlet.jsp.JspWriter;

import com.gouwudai.gobbs.common.Arguments;
import com.gouwudai.gobbs.service.ISysInfoService;
import com.gouwudai.gobbs.vo.SysInfoVO;
import com.litt.core.common.BeanManager;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.util.FormatDateTime;

/**
 * 
 * <b>标题:</b> 生成页面代码.
 * <pre><b>描述:</b> 
 *   版权信息、导航、分页条、错误页面的生成
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @since 2007-7-4
 * @version 1.0
 *
 */
public class HtmlConst {
	
	/**
	 * 版权所有的页角
	 * @return
	 */
    public static String CopyrightFoot() throws BusiException {  
    	ISysInfoService sysInfoService = (ISysInfoService)BeanManager.getBean("sysInfoService");
    	SysInfoVO sysInfo = sysInfoService.getSysInfoVO();
        
        StringBuilder sb = new StringBuilder();      
        sb.append("<table width=\"100%\" align=\"center\" cellpadding=\"2\" cellspacing=\"1\" class=\"tab\">");
        sb.append("<tr><td align=\"center\" height=\"25\" class=\"tab1\">");
        sb.append(sysInfo.getCopyright());
        sb.append("</td></tr></table>");
        sb.append("<div align=\"center\" class=\"smallTxt\">");
        sb.append("<a href=\""+sysInfo.getSysUrl()+"\" target=\"_blank\" title=\""+sysInfo.getSysName()+"\">");
        sb.append("<span class=\"smallTxt\">");
        sb.append(sysInfo.getSysName());
        sb.append("&nbsp;");
        sb.append(sysInfo.getVersion());
        sb.append("</span></a><br>");
        sb.append("<a href=\""+sysInfo.getSysUrl()+"\" target=\"_blank\">");
        sb.append("<img src=\"images/foot.gif\" alt=\""+sysInfo.getSysName()+"\" border=\"0\">");
        sb.append("</a>");
        sb.append("</div>");
        return sb.toString();
    }    
    
    /**
     * 导航栏，指明用户所在位置
     * @param url 
     * @return
     */
    public static String navMenu(String url){
    	StringBuilder sb = new StringBuilder();
    	sb.append("<strong>您的位置</strong>&gt;&gt;");
    	sb.append("<a href=\"index.jsp\" target=\"_parent\">社区首页</a>&gt;&gt;");
    	sb.append("<a href=\"main.jsp\">进站画面</a>&gt;&gt;");
    	sb.append(url);
    	return sb.toString();
    }
    
    /**
     * 导航栏，指明用户所在位置,没有超链
     * @param s 
     * @return
     */
    public static String navMenuNoLink(String s){
    	StringBuilder sb = new StringBuilder();
    	sb.append("<strong>您的位置</strong>&gt;&gt;社区首页&gt;&gt;进站画面&gt;&gt;");    	
    	sb.append(s);
    	return sb.toString();
    }    
    
    /**
     * 导航栏，指明用户所在位置
     * @param path  页面路径
     * @param s     页面描述
     * @return
     */
    public static String navMenu(String path, String s){
    	StringBuilder sb = new StringBuilder();
    	sb.append("<strong>您的位置</strong>&gt;&gt;");
    	sb.append("<a href=\"index.jsp\" target=\"_parent\">社区首页</a>&gt;&gt;");
    	sb.append("<a href=\"main.jsp\">进站画面</a>&gt;&gt;");
    	sb.append("<a href=\"");
    	sb.append(path);
    	sb.append("\" target=\"_self\">");
    	sb.append(s);
    	sb.append("</a>");
    	return sb.toString();
    }
    
    /**
     * 分类/版面的菜单导航，现只支持3层
     * @param classId
     * @param parentId
     * @param grandId
     * @param className
     * @param parentName
     * @param grandName
     * @return
     */
    public static String navClassMenu(Integer classId, Integer parentId, Integer grandId
    		, String className, String parentName, String grandName)
    {
    	StringBuilder sb = new StringBuilder();
    	if(grandId!=null&&grandId.intValue()>0){
    		sb.append("<a href=\"class.jsp?classId=");
    		sb.append(grandId);
    		sb.append("\">");
    		sb.append(grandName);
    		sb.append("</a>&gt;&gt;");
    	}
    	if(parentId!=null&&parentId.intValue()>0){
    		sb.append("<a href=\"class.jsp?classId=");
    		sb.append(parentId);
    		sb.append("\">");
    		sb.append(parentName);
    		sb.append("</a>&gt;&gt;");
    	}
    	sb.append("<a href=\"class.jsp?classId=");
    	sb.append(classId);
    	sb.append("\">");
    	sb.append(className);
    	sb.append("</a>");
    	return sb.toString();
    }    
    
    /**
     * 分页Html
     * @param totalCount 数据总数
     * @param pageNum 每页显示数量
     * @param pageIndex 当前页
     * @param url 
     * @return
     */
    public static String pageJump(int totalCount, int pageNum, int pageIndex,
			String url) {    	
    	int prevPage = pageIndex-1;	//上一页
    	int nextPage = pageIndex+1;	//下一页
		if (pageIndex < 1) {
			pageIndex = 1;
		}
		if (prevPage < 1) {
			prevPage = 1;
		}
		
		if (totalCount <= 0) {
			return "";
		}
		int iPage1 = 0, iPage2 = 0;
		int totalPage = 0; // 总页数
		if (totalCount % pageNum != 0) {
			totalPage = totalCount / pageNum + 1;
		} else {
			totalPage = totalCount / pageNum;
		}
		
		StringBuffer sb = new StringBuffer();
		sb.append("<table height=\"25\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tab\"><tr class=\"tab1\">");
		sb.append("<td class=\"tab\">&nbsp;<strong>");
		sb.append(totalPage);
		sb.append("</strong>&nbsp;</td>");
		if(pageIndex>1)
		{	
			sb.append("<td align=\"center\">");
			sb.append("&nbsp;<a title=\"返回首页\" href=\"" + url
					+ "pageIndex=1\">&lt;&lt;</a>&nbsp;");
			sb.append("</td>");
			sb.append("<td align=\"center\">");
			sb.append("<a title=\"返回上一页\" href=\"" + url + "pageIndex=" + prevPage
					+ "\">&lt;</a>&nbsp;");
			sb.append("</td>");
		}		
		if (pageIndex >= 10) {
			iPage2 = pageIndex + 8;
			iPage1 = pageIndex - 1;
			if (iPage2 > totalPage) {
				iPage2 = totalPage;
				iPage1 = totalPage - 9;
			}
		} else {
			if (totalPage > 10) {
				iPage2 = 10;
			} else {
				iPage2 = totalPage;
			}
			iPage1 = 1;
		}
		for (int i = iPage1; i <= iPage2; i++) {
			if (i == pageIndex) {
				sb.append("<td align=\"center\" class=\"tab3\">");
				sb.append("&nbsp;<a href=\"" + url + "pageIndex=" + i + "\">");
				sb.append("<span style=color:#bb0000><strong>" + i
						+ "</strong></span></a>&nbsp;");
				sb.append("</td>");
			} else {
				sb.append("<td align=\"center\"> ");
				sb.append("&nbsp;<a href=\"" + url + "pageIndex=" + i + "\">");
				sb.append("<strong>" + i + "</strong></a>&nbsp; ");
				sb.append("</td>");
			}
		}

		if(nextPage<=totalPage)	//如果跳到了最后一页则不显示跳到下一页
		{
			sb.append("<td align=\"center\">");
			sb.append("&nbsp;<a title=\"跳到下一页\" href=\"" + url + "pageIndex="
					+ nextPage + "\">&gt;</a>");
			sb.append("</td>");
			sb.append("<td align=\"center\">");
			sb.append("&nbsp;<a title=\"跳到尾页\" href=\"" + url + "pageIndex="
					+ totalPage + "\">&gt;&gt;</a>");
			sb.append("</td>");			
		}
		sb.append("</table>");
		return sb.toString();
	}    
    
    /**
     * 定制页面时间输出格式
     * @param sb
     * @return
     * @date 2006-11-3
     */
     public static String formatDateHtml(Date date)
     {
         if(date==null)
             return "";
         StringBuilder ret = new StringBuilder(200);
         //获得传入时间和当前时间相差的天数
         int days = Utility.getBetweenDays(new Date(),date);
         if(days==0)//今天
         {
        	 ret.append("<span style='font-size: 12px; color:#cc0000;font-weight: bold;'>今天</span>&nbsp;");
        	 ret.append("<span style='color:#990066'>"+FormatDateTime.formatTime(date));
        	 ret.append("</span>");
        	 return ret.toString();
         }
         else if(days==-1)//昨天
         {
        	 ret.append("<span style='font-size: 12px; color:#006600;font-weight: bold;'>昨天</span>&nbsp;");
        	 ret.append("<span style='color:#990066'>"+FormatDateTime.formatTime(date));
        	 ret.append("</span>"); 
        	 return ret.toString();
         }	
         else if(days==-2)//前天
         {
        	 ret.append("<span style='font-size: 12px; color:#2A00FF;font-weight: bold;'>前天</span>&nbsp;");
        	 ret.append("<span style='color:#990066'>"+FormatDateTime.formatTime(date));
        	 ret.append("</span>"); 
        	 return ret.toString();
         }	
         else
         {
        	 ret.append("<span style='color:#990066'>"+FormatDateTime.formatDateTime(date));
        	 ret.append("</span>"); 
        	 return ret.toString();
         }
     }    
    
    
     /**
      * 根据错误信息在表格中显示
      * @param errorMsg
      * @param tdCount TD个数，用来合并单元格
      * @return
      * @date 2006-09-25
      */
     public static String tableError(String errorMsg,int tdCount)
     {
     	if(errorMsg==null||errorMsg.equals(""))
     		return "";
     	StringBuilder sb = new StringBuilder();
     	sb.append("<tr><td align='center' colspan='"+tdCount+"'>");
     	sb.append("<font color='red'><strong>");    	
     	sb.append(errorMsg);
     	sb.append("</strong></font></td></tr>");
     	return sb.toString();    	
     }   
    
 	public static String errors(int errorId, String txt) throws BusiException{
		
		StringBuffer sb = new StringBuffer();
		sb.append("<html><head><title>信息提示</title>");
        sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"/"+Arguments.getContentPath()+"/club/css/defaultStyle.css\" >");
		sb.append("</head><body>");
		sb.append("<script language=\"javascript\" type=\"text/javascript\" src=\"/"+Arguments.getContentPath()+"scripts/common.js\"></script>");
		sb.append("<script language=\"javascript\" src=\"scripts/errors.js\"></script>");
		sb.append("<DIV class=\"line\"></DIV><DIV class=\"list_table_0\">");
		sb.append("<strong>您的位置</strong>&gt;&gt;<a href=\"index.jsp\" target=\"_parent\">社区首页</a>&gt;&gt;<a href=\"main.jsp\">进站画面</a>&gt;&gt;信息提示");
		sb.append("</DIV><DIV class=\"line\"></DIV><script language=\"javascript\">tabline(1,2,\"#ffffff\");</script>");
		sb.append("<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" class=\"tab\">");
		sb.append("<tr><td class=\"bg2\"><script language=\"JavaScript\">errTxt("+ errorId +");</script>");
		sb.append("<span style=\"color:#ff0000\">");
		sb.append(txt);
		sb.append("</span><hr size=\"1\">");
		sb.append("请认真查看社区帮助文档，如果您的问题仍然无法解决，请联系讨论组勤杂工！");
		sb.append("</td></tr><tr><td align=\"center\" class=\"bg2\">");
		sb.append("<a href=javascript:history.go(-1)>返回上一步＞＞＞</a></td></tr></table>");
		sb.append(CopyrightFoot());
		sb.append("</body></html>");
		return sb.toString();
	} 
 	
 	public static void errors(JspWriter out,int errorId, String txt) throws BusiException,IOException{
		 
        StringBuffer sb = new StringBuffer();
		sb.append("<html><head><title>信息提示</title>");
        sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"/"+Arguments.getContentPath()+"/club/css/defaultStyle.css\" >");
		sb.append("<link rel=\"stylesheet\" href=\"/"+Arguments.getContentPath()+"/thickbox/css/thickbox.css\" type=\"text/css\" media=\"screen\" />");
		sb.append("<script language=\"javascript\" type=\"text/javascript\" src=\"/"+Arguments.getContentPath()+"/scripts/common.js\"></script>");
		sb.append("<script language=\"javascript\" type=\"text/javascript\" src=\"/"+Arguments.getContentPath()+"/thickbox/scripts/jquery-compressed.js\"></script>");
		sb.append("<script language=\"javascript\" type=\"text/javascript\" src=\"/"+Arguments.getContentPath()+"/thickbox/scripts/thickbox.js\"></script>");
		sb.append("<script language=\"javascript\" src=\"/"+Arguments.getContentPath()+"/club/scripts/errors.js\"></script>");
		sb.append("</head><body>");		
		sb.append("<script language=\"javascript\" type=\"text/javascript\" src=\"/"+Arguments.getContentPath()+"/club/template/scripts/topBar.js\"></script>");		
		sb.append("<DIV class=\"line\"></DIV><DIV class=\"list_table_0\">");
		sb.append("<strong>您的位置</strong>&gt;&gt;<a href=\"/"+Arguments.getContentPath()+"/index.jsp\" target=\"_parent\">社区首页</a>&gt;&gt;<a href=\"/"+Arguments.getContentPath()+"/main.jsp\">进站画面</a>&gt;&gt;信息提示");
		sb.append("</DIV><DIV class=\"line\"></DIV><script language=\"javascript\">tabline(1,2,\"#ffffff\");</script>");
		sb.append("<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" class=\"tab\">");
		sb.append("<tr><td class=\"bg2\"><script language=\"JavaScript\">errTxt("+ errorId +");</script>");
		sb.append("<span style=\"color:#ff0000\">");
		sb.append(txt);
		sb.append("</span><hr size=\"1\">");
		sb.append("请认真查看社区帮助文档，如果您的问题仍然无法解决，请联系讨论组勤杂工！");
		sb.append("</td></tr><tr><td align=\"center\" class=\"bg2\">");
		sb.append("<a href=javascript:history.go(-1)>返回上一步＞＞＞</a></td></tr></table>");
		sb.append(CopyrightFoot());
		sb.append("</body></html>");
		out.clear();
		out.print(sb.toString());
		out.close();		
	}  	
    
//**************************************************************************  
    

}
